<?php
/**
 * 公共方法类
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/11/13
 * Time: 18:50
 */

namespace Home\Services;


class CommonService
{
    /**
     * 获取一年每周的开始日期和结束日期
     * @param $year
     * @return mixed
     */
    public static function get_week($year)
    {
        $year_start = $year . "-01-01";
        $year_end = $year . "-12-31";
        $startday = strtotime($year_start);
        if (intval(date('N', $startday)) != '1') {
            $startday = strtotime("next monday", strtotime($year_start)); //获取年第一周的日期
        }
        $year_mondy = date("Y-m-d", $startday); //获取年第一周的日期

        $endday = strtotime($year_end);
        if (intval(date('W', $endday)) == '7') {
            $endday = strtotime("last sunday", strtotime($year_end));
        }

        //$num = intval(date('W', $endday));
        $num = 52;//一年52周
        for ($i = 1; $i <= $num; $i++) {
            $j = $i - 1;
            $start_date = date("Y-m-d", strtotime("$year_mondy $j week "));

            $end_day = date("Y-m-d", strtotime("$start_date +6 day"));

            $week_array[$i] = array(
                $start_date, $end_day);
        }
        return $week_array;
    }

    public static function array_sort($array, $field)
    {
        $sort = array(
            'direction' => 'SORT_DESC',//排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
            'field' => $field,//排序字段
        );
        $arrSort = array();
        foreach ($array AS $uniqid => $row) {
            foreach ($row AS $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if ($sort['direction']) {
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
        }
        return $array;
    }
}